package com.siwei.mes.service.experiment.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentMessage;
import com.siwei.mes.enums.ExperimentMessageStatusEnum;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.mapper.experiment.ExperimentMessageMapper;
import com.siwei.mes.service.experiment.ExperimentMessageService;
import com.siwei.mes.service.system.AuthService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 实验台账消息通知表(ExperimentMessage)表服务实现类
 *
 * @author makejava
 * @since 2024-07-26 22:31:07
 */
@Service
public class ExperimentMessageServiceImpl implements ExperimentMessageService {
    @Resource
    private ExperimentMessageMapper experimentMessageMapper;
    @Resource
    private AuthService authService;

    @Override
    public Long create(ExperimentMessage entity) {
        experimentMessageMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ExperimentMessage entity) {
        ExperimentMessage experimentMessage = experimentMessageMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentMessage == null, "实验台账消息通知表信息不存在");
        experimentMessageMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentMessage experimentMessage = experimentMessageMapper.selectByPrimaryKey(id);
        assertTrue(experimentMessage == null, "实验台账消息通知表信息不存在");
        experimentMessageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ExperimentMessage> getPage(PageQuery<ExperimentMessage, ExperimentMessage> query) {
        ExperimentMessage searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentMessageMapper.getList(searchParam));
    }

    @Override
    public ExperimentMessage loadById(Long id) {
        return experimentMessageMapper.loadById(id);
    }

    @Override
    public List<ExperimentMessage> getAll(ExperimentMessage entity) {
        String projectJson = authService.getLoginUserProjectJson();
        JSONArray jsonArray = JSONUtil.parseArray(projectJson);
        if (!jsonArray.isEmpty()) {
            List<Integer> experimentTypeList = new ArrayList<>();
            for (Object obj : jsonArray) {
                Map<String, Object> projectJsonMap = BeanUtil.beanToMap(obj);
                experimentTypeList.add(Integer.parseInt(projectJsonMap.get("no").toString()));
            }
            List<Integer> commonList = filterCommonElements(experimentTypeList, entity.getExperimentTypeList());
            if (CollectionUtils.isEmpty(commonList)) {
                return new ArrayList<>();
            }
            if (ObjectUtil.isNotEmpty(entity.getId())) {
                ExperimentMessage experimentMessage = experimentMessageMapper.selectByPrimaryKey(entity.getId());
                if (experimentMessage != null) {
                    entity.setStartTime(experimentMessage.getCreateTime());
                }
            }
            entity.setExperimentTypeList(commonList);
            List<ExperimentMessage> experimentMessageList = experimentMessageMapper.getList(entity);
            for (ExperimentMessage experimentMessage : experimentMessageList) {
                experimentMessage.setExperimentTypeName(MaterialsEnum.selectByCode(experimentMessage.getExperimentType().intValue()).getName());
            }
            return experimentMessageList;
        } else {
            return new ArrayList<>();
        }
    }

    public static List<Integer> filterCommonElements(List<Integer> list1, List<Integer> list2) {
        List<Integer> result = new ArrayList<>(list1); // 创建一个新列表，包含list1的所有元素
        result.retainAll(list2); // 保留同时存在于list1和list2中的元素
        return result;
    }

    @Override
    public void read(Long id) {
        ExperimentMessage entity = experimentMessageMapper.selectByPrimaryKey(id);
        assertTrue(entity == null, "实验台账消息通知信息不存在");
        entity.setReadPersonId(authService.getLoginUserId());
        entity.setReadPersonName(authService.getLoginUserName());
        entity.setReadTime(new Date());
        entity.setState(ExperimentMessageStatusEnum.READ.getCode());
        experimentMessageMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void saveExperimentMessage(Experiment entity) {
        ExperimentMessage experimentMessage = new ExperimentMessage();
        experimentMessage.setExperimentId(entity.getId());
        experimentMessage.setExperimentNo(entity.getExperimentNo());
        experimentMessage.setExperimentType(Long.valueOf(entity.getExperimentType()));
        experimentMessage.setState(1);
        experimentMessage.setCreateTime(new Date());
        experimentMessageMapper.insert(experimentMessage);
    }
}
